package com.example.attendancesystem.Service.Imp;


import com.example.attendancesystem.Mapper.AdminsMapper;
import com.example.attendancesystem.Mapper.EmployeesMapper;
import com.example.attendancesystem.Mapper.LeaveRowMapper;
import com.example.attendancesystem.Mapper.NewsMapper;
import com.example.attendancesystem.Models.*;
import com.example.attendancesystem.Service.IndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class IndexServiceImp implements IndexService {

    @Autowired
    private AdminsMapper adminsMapper;

    @Autowired
    private EmployeesMapper employeesMapper;

    @Autowired
    private LeaveRowMapper leaveRowMapper;
    @Autowired
    private NewsMapper newsMapper;


    /**
     * 分页查询所有员工信息
     *
     * @param offset 第几条记录开始查询
     * @param limit  每页限制条数
     * @return DataV0对象
     */
    @Override
    public DataEmp getAllEmp(Integer offset, Integer limit) {
        DataEmp dataEmp = new DataEmp();
        dataEmp.setCode(0);
        dataEmp.setMsg("");
        dataEmp.setCount(employeesMapper.getEmpSum());
        List<Employee> employeeList = employeesMapper.getAllEmp(offset, limit);
        dataEmp.setData(employeeList);
        return dataEmp;
    }

    @Override
    public Boolean checkOPRTPass(String adminNumber, String operaPass) {
        String getPass = adminsMapper.getOPPass(adminNumber);
        return operaPass.equals(getPass);
    }

    @Override
    public Boolean delEmpByNumber(String empNumber) {
        int res = employeesMapper.delEmpByNumber(empNumber);
        return res == 1;
    }

    @Override
    public DataEmp queryEmpByNameOrNumber(Integer page, Integer limit, String emp) {
        DataEmp dataEmp = new DataEmp();
        dataEmp.setCode(0);
        dataEmp.setMsg("");
        List<Employee> employeeList = employeesMapper.queryEmpByNameOrNumber(emp);
        dataEmp.setData(employeeList);
        dataEmp.setCount(employeeList.size());
        return dataEmp;
    }

    @Override
    public Employee getEmpInfoByNumber(String empNumber) {
        Employee employee;
        employee = employeesMapper.getEmpByEmpNumber(empNumber).get(0);
        return employee;
    }

    @Override
    public DataLeaveRow getEmpLeaveRequest() {
        DataLeaveRow dataLeaveRow = new DataLeaveRow();
        dataLeaveRow.setCode(0);
        dataLeaveRow.setMsg("");
        List<LeaveRow> leaveRows = leaveRowMapper.getEmpLeaveRequest();
        dataLeaveRow.setData(leaveRows);
        dataLeaveRow.setCount(leaveRows.size());
        return dataLeaveRow;
    }

    @Override
    public Integer getLeaveRequestSum() {
        Integer sum;
        sum = leaveRowMapper.getLeaveRequestSum();
        return sum;
    }

    @Override
    public Boolean dealLeaveRequest(Integer result, Integer requestId) {
        boolean flag = false;
        Integer res = leaveRowMapper.dealLeaveRequest(result, requestId);
        if (res == 1) {
            flag = true;
        }
        return flag;
    }

    @Override
    public Employee addEmp(Employee addEmp) {
        Employee newEmp = new Employee();
        //1生成工号 格式部门的缩写 + 当前日期年月日 + 两位随机数
        //1-1
        String str1 = null;
        switch (addEmp.getDepartment()) {
            case "服务部":
                str1 = "FW";
                break;
            case "办公室":
                str1 = "BG";
                break;
            case "财务部":
                str1 = "CW";
                break;
            case "企划部":
                str1 = "QH";
                break;
            case "产品部":
                str1 = "CP";
                break;
            case "后勤部":
                str1 = "HQ";
                break;
            default:
                break;
        }
        SimpleDateFormat date = new SimpleDateFormat("yyMMdd");
        String str2 = date.format(new Date());
        //查询员工号是否已经存在
        int empNumberNum, num;
        String str3, empNumber;
        do {
            //1.3 生成两位随机数
            num = (new Random().nextInt(99 - 10 + 1) + 10);
            str3 = String.valueOf(num);
            empNumber = str1 + str2 + str3;
            //查询员工号是否已经存在
            empNumberNum = employeesMapper.getEmpNumberSum(empNumber);
        } while (empNumberNum != 0);
        addEmp.setEmpNumber(empNumber);
        //生成密码
        Random random = new Random();
        StringBuilder empPassword = new StringBuilder();
        for (int i = 0; i < 8; i++) {
            // 1、2随机生成整数  如果 1就生成字符 2则生成数字
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            //生成字符
            if (charOrNum.equals("char")) {
                // 判断是生成大写还是小写
                int choice = random.nextInt(2) == 0 ? 65 : 97;
                empPassword.append((char) (choice + random.nextInt(26)));
            } else { //生成数字
                empPassword.append(random.nextInt(10));
            }
        }
        addEmp.setEmpPassword(String.valueOf(empPassword));
        //获取加入时间
        SimpleDateFormat addDate = new SimpleDateFormat("yyyy-MM-dd");
        String addTime = addDate.format(new Date());
        addEmp.setAddTime(addTime);
        addEmp.setQues("你的电话号码是");
        addEmp.setAns("123456");
        int res = employeesMapper.addEmp(addEmp);
        if (res == 1) {
            newEmp = employeesMapper.getEmpByEmpNumber(empNumber).get(0);
        }
        return newEmp;
    }

    @Override
    public Boolean updateEmpPass(String empPass, String empQues, String empAns, String empNumber) {
        int res = employeesMapper.updateEmpPass(empPass, empQues, empAns, empNumber);
        return res == 1;
    }


    @Override
    public List<Employee> getAllEmpDown() {
        List<Employee> employeeList = employeesMapper.getAllEmpDown();
        return employeeList;
    }

    @Override
    public boolean updateEmpInfo(Employee employee) {
        String oldEmpNumber = employee.getEmpNumber();
        String newEmpNumber = employee.getEmpNumber();
        String empNumberDate = employee.getEmpNumber().substring(2);
        switch (employee.getDepartment()) {
            case "服务部":
                newEmpNumber = "FW" + empNumberDate;
                break;
            case "办公室":
                newEmpNumber = "BG" + empNumberDate;
                break;
            case "财务部":
                newEmpNumber = "CW" + empNumberDate;
                break;
            case "企划部":
                newEmpNumber = "QH" + empNumberDate;
                break;
            case "产品部":
                newEmpNumber = "CP" + empNumberDate;
                break;
            case "后勤部":
                newEmpNumber = "HQ" + empNumberDate;
                break;
            default:
                break;
        }
        employee.setEmpNumber(newEmpNumber);
        int res = employeesMapper.updateEmpInfo(employee, oldEmpNumber);
        return res == 1;
    }

    @Override
    public Boolean addNews(String title, String author, String text) {
        SimpleDateFormat nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String addTime = nowTime.format(new Date());
        int res = newsMapper.addNews(title,author,text,addTime);
        return res == 1;
    }

    @Override
    public News getNewsById(Integer newsId) {
        return newsMapper.getNewsById(newsId);
    }

    @Override
    public void updateEmpImg(Integer newsId, String savaImgName) {
        newsMapper.updateEmpImg( newsId,savaImgName);
    }
}
